require('es6-shim')
require('es7-shim')
require('./utils/controlbox')

import Vue from 'vue'
import VueRouter from 'vue-router'

import {echo, VueEcho} from './utils/echo.js'
import AlertData from './utils/echo-alert-data.js'

Vue.use(VueEcho)

Vue.directive('echo-page', {
  params: ['page-name'],
  bind: function () {
    if (this.el.__vue__ === undefined) {
      console.error('echo-page vue is undefine ' + this)
    } else {
      this.el.__vue__.$pageName = this.params.$pageName
      echo.addvm(this.params.pageName, this.el.__vue__)
    }
  },
  unbind: function () {
    echo.remove(this.params.pageName)
  }
})

Vue.filter('alertText', function (alertId) {
  return AlertData.getAlertText(alertId)
})

Vue.filter('alertIcon', function (alertId) {
  return AlertData.getAlertIcon(alertId)
})

Vue.filter('toLocalTime', function (time) {
  function checkTime (val) {
    if (val < 10) {
      return '0' + val
    }
    return val
  }
  let h = time.getHours()
  let m = time.getMinutes()
  let s = time.getSeconds()
  h = checkTime(h)
  m = checkTime(m)
  s = checkTime(s)
  return h + ':' + m + '\'' + s
})

import App from './App'

Vue.use(VueRouter)

var router = new VueRouter()
var app = Vue.extend(App)
echo.router = router

router.map({
  '/pwr': {
    component: require('./pages/pwr.vue')
  },
  'disp': {
    component: require('./pages/disp.vue')
  },
  'menu': {
    component: require('./pages/menu.vue')
  },
  'logBook': {
    component: require('./pages/log-book.vue')
  },
  'alert-log': {
    component: require('./pages/alert-log.vue')
  },
  'active-alert': {
    component: require('./pages/active-alert.vue')
  }
})

router.start(app, '#app')
